package com.newsmanagement.controller.backstage;

import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author xbr
 * 2017.12.04 11:24
 * 后台系统 角色管理
 */
@Controller
@RequestMapping("roleBack")
public class RoleBackstageController {

    @Autowired
    private IRoleService roleService;

    /**
     * 添加角色信息
     * @param userId 创建人id
     * @param roleName 角色名称
     * @return ReturnedMessages
     * 测试：
     * http://localhost:8080/roleBack/add?userId=1&roleName=juesemingcheng 用于添加
     * http://localhost:8080/roleBack/add?userId=1&roleName 测试名称
     */
    @RequestMapping("add")
    @ResponseBody
    public ReturnedMessages addRole(
            @RequestParam(value = "userId") Integer userId ,
            @RequestParam(value = "roleName") String roleName
    ){

        return roleService.addRole(userId ,roleName);
    }

    /**
     * 修改角色
     * @param userId 操作的用户id
     * @param roleId 被操作的角色id
     * @param roleName 角色名称
     * @return ReturnedMessages
     * http://localhost:8080/roleBack/update?userId=1&roleName=juesemingcheng14&roleId=14
     */
    @RequestMapping("update")
    @ResponseBody
    public ReturnedMessages updateRole(
            @RequestParam(value = "userId") Integer userId ,
            @RequestParam(value = "roleId") Integer roleId ,
            @RequestParam(value = "roleName") String roleName
    ){

        return roleService.updateRole(userId , roleId ,roleName);
    }


    /**
     * 删除角色
     * @param userId 操作的用户id
     * @param roleId 被操作的角色id
     * @return ReturnedMessages
     * http://localhost:8080/roleBack/delete?userId=55&roleId=14
     */
    @RequestMapping("delete")
    @ResponseBody
    public ReturnedMessages deleteRole(
            @RequestParam(value = "userId") Integer userId ,
            @RequestParam(value = "roleId") Integer roleId
    ){

        return roleService.deleteRole(userId , roleId);
    }

    /**
     * 设置角色对应可请求资源
     * @param userId 操作的用户id
     * @param roleId 被操作的角色id
     * @param resourceIds 资源id集：使用“，”隔开
     * @return ReturnedMessages
     * http://localhost:8080/roleBack/addResource?userId=2&roleId=14&resourceIds=,1,2,ewtgwe   ,
     */
    @RequestMapping("addResource")
    @ResponseBody
    public ReturnedMessages addResource(
            @RequestParam(value = "userId") Integer userId ,
            @RequestParam(value = "roleId") Integer roleId ,
            @RequestParam(value = "resourceIds") String resourceIds
    ){

        return roleService.addResource(userId , roleId ,resourceIds);
    }


}
